from django.http import FileResponse
from django.shortcuts import render,redirect,HttpResponse
import subprocess as sp
import socket
from .models import Lives
from django.urls import reverse_lazy
from django.views.generic import TemplateView, DetailView
import socket


# Create your views here.


import sys, os, time, signal
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot

import subprocess as sp
import webbrowser



# class App(QWidgetidget):
#
#     info = {}
#
#     def __init__(self):
#         super().__init__()
#         self.title = "看直播"
#         self.left = 20
#         self.top = 20
#         self.width = 320
#         self.height = 200
#         self.initUI()
#
#
#     def initUI(self):
#         self.setWindowTitle(self.title)
#         self.setGeometry(self.left, self.top, self.width, self.height)
#         """在窗体内创建button对象"""
#         self.button1 = QPushButton("开启我的直播", self)
#         """方法setToolTip在用户将鼠标停留在按钮上时显示的消息"""
#         self.button1.setToolTip("put me on video")
#         """按钮坐标x = 100, y = 70"""
#         self.button1.move(50, 50)
#         """按钮与鼠标点击事件相关联"""
#         self.button1.clicked.connect(self.on_click1)
#
#
#
#         # self.button2 = QPushButton("观看直播", self)
#         # """方法setToolTip在用户将鼠标停留在按钮上时显示的消息"""
#         # self.button2.setToolTip("watch little sister")
#         # """按钮坐标x = 100, y = 70"""
#         # self.button2.move(50, 100)
#         # """按钮与鼠标点击事件相关联"""
#         # self.button2.clicked.connect(self.on_click2)
#
#         self.button3 = QPushButton("关闭摄像头", self)
#         """方法setToolTip在用户将鼠标停留在按钮上时显示的消息"""
#         self.button3.setToolTip("watch little sister")
#         """按钮坐标x = 100, y = 70"""
#         self.button3.move(50, 150)
#         """按钮与鼠标点击事件相关联"""
#         self.button3.clicked.connect(self.on_click3)
#
#
#
#         self.show()
#
#     """创建鼠标点击事件"""
#
#     @pyqtSlot()
#     def on_click1(self,request):
#         hostname = socket.gethostname()
#         # ip = "127.0.0.1"
#         ip = "10.2.0.174"
#         # if nginx is None:
#         #     sp.run(['nginx'])
#         rtmpurl = 'rtmp://' + str(ip) + ':1935/hls/camera'
#         url = 'http://' + str(ip) + ':8080/hls/camera.m3u8'
#
#         command = ['ffmpeg',
#                    '-f', 'avfoundation',
#                    '-framerate', '30',
#                    '-video_size',
#                    '1280x720', '-i',
#                    "0:0", '-vcodec',
#                    'libx264', '-preset',
#                    'ultrafast', '-acodec',
#                    'libmp3lame', '-ar',
#                    '44100', '-ac',
#                    '1', '-f',
#                    'flv', rtmpurl]
#         pipe = sp.Popen(command, stdin=sp.PIPE)
#         self.info['pipe'] = pipe
#         self.info['url'] = url
#         user = request.user
#         Lives.objects.create(user=user, video=url)
#
#
#
#
#     # @pyqtSlot()
#     # def on_click2(self):
#     #     # _,url = self.on_click1
#     #     # print(url)
#     #     webbrowser.open(self.info['url'], new=0, autoraise=True)
#     #     webbrowser.get()
#     #     sp.run(['python', '../video-contrib-hls/index.html'])
#
#
#
#     @pyqtSlot()
#     def on_click3(self):
#         os.kill(self.info['pipe'].pid, signal.SIGKILL)
#         print('xxxx3333')


class Download(TemplateView):
    template_name = "bootstrap/choice.html"

    # def get_context_data(self, **kwargs):
    #     user = self.request.user
    #     hostname = socket.gethostname()
    #     ip = socket.gethostbyname(hostname)
    #     url = 'http://' + str(ip) + ':1935/hls/camera.m3u8'
    #     Lives.objects.create(user=user, video=url)
    #     return super().get_context_data(**kwargs)



def myshow(request):
    fp = open("./client/TEST1.py", "rb")
    response = FileResponse(fp)
    response['Content-Type'] = 'application/octet-stream'
    response['Content-Disposition'] = 'attachment;filename="live.py"'

    user = request.user
    hostname = socket.gethostname()
    ip = socket.gethostbyname(hostname)
    url = 'http://' + str(ip) + ':8080/hls/camera.m3u8'
    Lives.objects.create(user=user, video=url)
    addr=request.META['HTTP_REFERER']
    addr = addr[:-10] + 'click/'
    redirect(addr)
    return response


class Click(TemplateView):
    template_name = 'bootstrap/second.html'

    def get_context_data(self, **kwargs):
        kwargs['all_video'] = Lives.objects.all()
        return super().get_context_data(**kwargs)



class Singlelive(DetailView):

    template_name = 'bootstrap/videojs-contrib-hls/index.html'
    model = Lives
    context_object_name = 'single_detail'
    # user_register_form = UserRegisterForm()
    # user_login_form = UserLoginForm()

    # def get_context_data(self, **kwargs):
        # kwargs['user_comments'] =Comments.objects.filter(item__id=kwargs['object'].pk).order_by('-comment_time')
        # return super().get_context_data(**kwargs)